package com.example.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.constant.RedisConstant;
import com.example.common.redis.RedisUtils;
import com.example.common.utils.StringUtils;
import com.example.system.domain.entity.Process;
import com.example.system.domain.entity.*;
import com.example.system.mapper.*;
import com.example.system.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DictServiceImpl implements DictService {
    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ImageMapper imageMapper;
    @Autowired
    private FriendMapper friendMapper;
    @Autowired
    private ScoreMapper scoreMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private ProcessMapper processMapper;
    @Autowired
    private ActivationMapper activationMapper;

    /**
     * 创建字典缓存
     */
    @Override
    public void createDictRedis() {
        LambdaQueryWrapper<Dict> lqw = new LambdaQueryWrapper<>();
        List<Dict> dictList = dictMapper.selectList(lqw);
        String key = RedisConstant.dict;
        Map<String, List<Dict>> map = dictList.stream().collect(Collectors.groupingBy(Dict::getType));
        map.forEach((K, V) -> redisUtils.set(key + K, V));
        redisUtils.set(key + RedisConstant.db_id, getDBId());
    }

    /**
     * 获取字典列表
     */
    @Override
    public List<Dict> getList(String dict) {
        String key = RedisConstant.dict;
        return (List<Dict>) redisUtils.get(key + dict);
    }

    /**
     * 获取数据库ID-分组
     */
    public Map<String, Integer> getDBId() {
        String[] db_ids = RedisConstant.db_ids;
        Map<String, Integer> map = new HashMap<>();
        for (String s : db_ids) map.put(s, getDBId(s));
        return map;
    }

    /**
     * 获取字典-数据库主键
     */
    public int getDBId(String db) {
        switch (db) {
            case "user":
                List<User> users = userMapper.selectList(new LambdaQueryWrapper<>());
                if (StringUtils.lisEmpty(users)) {
                    return 1;
                } else {
                    Integer id = users.stream().max(Comparator.comparing(User::getUserId)).get().getUserId();
                    return id + 1;
                }
            case "role":
                List<Role> roles = roleMapper.selectList(new LambdaQueryWrapper<>());
                if (StringUtils.lisEmpty(roles)) {
                    return 1;
                } else {
                    Integer id = roles.stream().max(Comparator.comparing(Role::getRoleId)).get().getRoleId();
                    return id + 1;
                }
            case "menu":
                List<Menu> menus = menuMapper.selectList(new LambdaQueryWrapper<>());
                if (StringUtils.lisEmpty(menus)) {
                    return 1;
                } else {
                    Integer id = menus.stream().max(Comparator.comparing(Menu::getMenuId)).get().getMenuId();
                    return id + 1;
                }
            case "item":
                List<Item> items = itemMapper.selectList(new LambdaQueryWrapper<>());
                if (StringUtils.lisEmpty(items)) {
                    return 1;
                } else {
                    Integer id = items.stream().max(Comparator.comparing(Item::getId)).get().getId();
                    return id + 1;
                }
            case "project":
                List<Project> projects = projectMapper.selectList(new LambdaQueryWrapper<>());
                if (StringUtils.lisEmpty(projects)) {
                    return 1;
                } else {
                    Integer id = projects.stream().max(Comparator.comparing(Project::getId)).get().getId();
                    return id + 1;
                }
            case "dict":
                List<Dict> dicts = dictMapper.selectList(new LambdaQueryWrapper<>());
                if (StringUtils.lisEmpty(dicts)) {
                    return 1;
                } else {
                    Integer id = dicts.stream().max(Comparator.comparing(Dict::getId)).get().getId();
                    return id + 1;
                }
            case "image":
                List<Image> images = imageMapper.selectList(new LambdaQueryWrapper<>());
                if (StringUtils.lisEmpty(images)) {
                    return 1;
                } else {
                    Integer id = images.stream().max(Comparator.comparing(Image::getId)).get().getId();
                    return id + 1;
                }
            case "friend":
                List<Friend> friends = friendMapper.selectList(new LambdaQueryWrapper<>());
                if (StringUtils.lisEmpty(friends)) {
                    return 1;
                } else {
                    Integer id = friends.stream().max(Comparator.comparing(Friend::getFriendId)).get().getFriendId();
                    return id + 1;
                }
            case "score":
                List<Score> scores = scoreMapper.selectList(new LambdaQueryWrapper<>());
                if (StringUtils.lisEmpty(scores)) {
                    return 1;
                } else {
                    Integer id = scores.stream().max(Comparator.comparing(Score::getId)).get().getId();
                    return id + 1;
                }
            case "user_role":
                List<UserRole> userRoles = userRoleMapper.selectList(new LambdaQueryWrapper<>());
                if (StringUtils.lisEmpty(userRoles)) {
                    return 1;
                } else {
                    Integer id = userRoles.stream().max(Comparator.comparing(UserRole::getId)).get().getId();
                    return id + 1;
                }
            case "process":
                List<Process> processes = processMapper.selectList(new LambdaQueryWrapper<>());
                if (StringUtils.lisEmpty(processes)) {
                    return 1;
                } else {
                    Integer id = processes.stream().max(Comparator.comparing(Process::getId)).get().getId();
                    return id + 1;
                }
            case "activation":
                List<Activation> activations = activationMapper.selectList(new LambdaQueryWrapper<>());
                if (StringUtils.lisEmpty(activations)) {
                    return 1;
                } else {
                    Integer id = activations.stream().max(Comparator.comparing(Activation::getId)).get().getId();
                    return id + 1;
                }
            default:
                return 0;
        }
    }

}
